package dao;

import entity.Countries;
import util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CountryDao {

    public List<Countries> findAll(){
        List<Countries> cts=new ArrayList<>();
        ResultSet rs= DBUtil.find("select * from countries");
        ResultSetToList(cts, rs);
        return cts;
    }

    private void ResultSetToList(List<Countries> cts, ResultSet rs) {
        try {
            while(rs.next()){
                Countries c=new Countries();
                c.setCountryId(rs.getString(1));
                c.setCountryName(rs.getString(2));
                c.setRegion_id(rs.getInt(3));
                cts.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Countries> findByName(String name){
        String sql="select * from countries where country_name like ?";
        List<Countries> cts=new ArrayList<>();
        ResultSet rs= DBUtil.find(sql,name);
        ResultSetToList(cts, rs);
        return cts;
    }
}
